#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>


#define OK     (0)
#define ERROR  (-1)
#define PATH "/dev/ttymxc2"

int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
    struct termios newtio;
    if (tcgetattr(fd, &newtio) != 0) {
        perror("SetupSerial 1");
        return -1;
    }
    newtio.c_cflag &= ~CSIZE; //字符长度掩码。取值为：CS5，CS6，CS7或CS8
    switch(nBits)
    {
    case 5:
        newtio.c_cflag |= CS5;
        break;
    case 6:
        newtio.c_cflag |= CS6;
        break;
    case 7:
        newtio.c_cflag |= CS7;
        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }

    switch(nEvent)
    {
    case 'O':
        newtio.c_cflag |= PARENB;
        newtio.c_cflag |= PARODD;  
        newtio.c_iflag |= (INPCK | ISTRIP); 
        break;
    case 'E':
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        break;
    case 'N': 
        newtio.c_cflag &= ~PARENB;
        break;
    }

    switch(nSpeed)
    {
    case 2400:
        cfsetispeed(&newtio, B2400);
        cfsetospeed(&newtio, B2400);
        break;
    case 4800:
        cfsetispeed(&newtio, B4800);
        cfsetospeed(&newtio, B4800);
        break;
    case 9600:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    case 115200:
        cfsetispeed(&newtio, B115200);
        cfsetospeed(&newtio, B115200);
        break;
    case 460800:
        cfsetispeed(&newtio, B460800);
        cfsetospeed(&newtio, B460800);
        break;
    default:
        cfsetispeed(&newtio, B115200);
        cfsetospeed(&newtio, B115200);
        break;
    }

    if(nStop == 1)
        newtio.c_cflag &=  ~CSTOPB; 
    else if (nStop == 2)
        newtio.c_cflag |=  CSTOPB;

    newtio.c_cc[VTIME]  = 0;  // 读取串口数据等待的时间
    newtio.c_cc[VMIN] = 1;    // 最少读取到1个字节的数据才返回
    newtio.c_cflag  |=  CLOCAL | CREAD;
    newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    newtio.c_oflag &= ~OPOST;
    newtio.c_oflag &= ~(ONLCR | OCRNL);
    newtio.c_iflag &= ~(ICRNL | INLCR);
    newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
    
    tcflush(fd,TCIFLUSH);
    if((tcsetattr(fd, TCSANOW, &newtio)) != 0) {
        perror("com set error");
        return -1;
    }
    printf("set done!\n\r");
    return 0;
}
/*  选项
 *  hex-- 十六进制
 *  str-- 字符
 */
int main(int argc, char* argv[])
{
    int fd, ret, i = 0;
    int hex;
    char ch;

    if (2 != argc) {
        printf("input parameter error\n");
        return ERROR;
    }

    if (0 == strcmp("hex", argv[1]))
        hex = 1;
    else if (0 == strcmp("str", argv[1]))
        hex = 0;
    else {
        printf("option error\n");
        return ERROR;
    }
    fd = open(PATH, O_RDWR | O_NOCTTY);
    if (fd <= 0) {
        printf("open error\n");
        return ERROR;
    }
    printf("open %s success\n", PATH);
    ret = set_opt(fd, 115200, 8, 'N', 1);
    if (ret == -1) {
        printf("set_opt error\n");
        return ERROR;
    }

    while (1) { 
        ret = read(fd, &ch, sizeof(ch));
        if (ret < 0) {
            printf("read error\n");
            return ERROR;
        }
        // 读取到数据，打印并写回
        if (ret == sizeof(ch)) {
            if (1 == hex) {
                printf("%2X ", (unsigned char)ch);
                if (0 == ((i + 1) % 0xF))
                    printf("\n");
                ++i;
            }
            else
                printf("%c", ch);
            ret = write(fd, &ch, sizeof(ch));
            if (ret < 0) {
                printf("write error\n");
                return ERROR;
            }              
        }          
    }
    close(fd);
    return 0;
}
